package com.nitty.cash.loop.ui.presenter

import com.nitty.cash.loop.common.CommonPresenter
import com.nitty.cash.loop.configs.NittyApiPath
import com.nitty.cash.loop.configs.NittyApiUrl
import com.nitty.cash.loop.configs.NittyApiUrl.addCommonParams
import com.nitty.cash.loop.request.IHttpCallback
import com.nitty.cash.loop.ui.contract.IViewContract
import com.nitty.cash.loop.utils.helper.NittyCommentDialogHelper
import org.json.JSONObject

/**
 * NittyCash
 * @Author wwdeng
 * @CreateTime: 2024/9/27 15:38
 * @Annotation：订单详情
 */
class OrderDetailsPresenter(private val iView: IViewContract.IOrderDetailsView): CommonPresenter(iView) {

    /**
     * 获取贷款信息
     */
    fun loadOrderDetailsAction(){
        val httpUrl = NittyApiUrl.getHttpUrl()
        val params = JSONObject().addCommonParams().apply {
            put("xd304965bcd9478080",NittyApiPath.NITTY_PATH.NITTY_API_LOAN_APPLY_PATH)
            put("g82a","YES")
            put("t8812858f1c5484a8",iView.getOrderId())
        }
        doHttpPostAction(httpUrl,params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                val obj = data.optJSONObject("obj")
                if (obj != null) {
                    //显示共用View
                    iView.showOrderDetailsCommonViews(obj)
                    val orderStatus = obj.optString("status")
                    when(orderStatus) {
                        "IN_REVIEW" -> {//审核中
                            iView.showOrderDetailsReviewingViews()
                            loadOrderDetailsWalletListAction()
                        }
                        "LENDING" -> {//放款中
                            iView.showOrderDetailsDisbursingViews()
                            loadOrderDetailsWalletListAction()
                        }
                        "REJECT" -> {//已拒绝
                            iView.showOrderDetailsRejectedViews(obj)
                        }
                        else -> { //待还款
                            iView.showOrderDetailsRepayViews()
                        }
                    }
                }
            }

            override fun onRequestError(error: JSONObject) {
            }

            override fun onRequestFinish() {
                iView.hiddenNittyLoading()
            }
        })
    }

    /**
     * 获取账户列表
     */
    fun loadOrderDetailsWalletListAction(){
        val httpUrl = NittyApiUrl.getHttpUrl()
        val params = JSONObject().addCommonParams().apply {
            put("xd304965bcd9478080", NittyApiPath.NITTY_PATH.NITTY_API_ACCOUNT_LIST_PATH)
        }
        doHttpPostAction(httpUrl, params, object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                val array = data.optJSONArray("obj")
                val accountList = arrayListOf<JSONObject>()

                if (array != null && array.length() > 0) {
                    for (i in 0..<array.length()) {
                        val item = array.optJSONObject(i)
                        item.put("isEdit", false)
                        accountList.add(item)
                    }
                }
                iView.showOrderDetailsWalletList(accountList)
            }
            override fun onRequestError(error: JSONObject) {
            }
            override fun onRequestFinish() {
            }
        })
    }

    /**
     * 只有审核中或放款中需要调此接口
     * 获取评分配置
     */
    fun loadScoreCommentAction() {
        val httpUrl = NittyApiUrl.getHttpUrl()
        val params = JSONObject().addCommonParams().apply {
            put("xd304965bcd9478080", NittyApiPath.NITTY_PATH.NITTY_API_STAR_COMMENT_PATH)
        }
        doHttpPostAction(httpUrl,params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                val obj = data.optJSONObject("obj")
                if (obj != null) {
                    NittyCommentDialogHelper.showStarDialog(iView.getAct(),obj,true)
                }
            }

            override fun onRequestError(error: JSONObject) {
            }

            override fun onRequestFinish() {
            }
        })
    }
}